home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / plug-ins / common / gradmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-02-09  |  7.9 KB  |  298 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * Gradient Map plug-in
  5.  * Copyright (C) 1997 Eiichi Takamori <taka@ma1.seikyou.ne.jp>
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20.  */
  21.  
  22. /*
  23.  * version 1.07
  24.  * This plug-in requires GIMP v0.99.10 or above.
  25.  *
  26.  * This plug-in maps the image using active gradient. (See help_string
  27.  * in query ()).
  28.  *
  29.  *    Eiichi Takamori <taka@ma1.seikyou.ne.jp>
  30.  *    http://ha1.seikyou.ne.jp/home/taka/gimp/
  31.  *
  32.  * Changes from version 1.06 to version 1.07:
  33.  * - If layer is RGBA or GRAYA (partially transparent), preserve
  34.  *   the alpha channel instead of making invisible pixels visible.
  35.  *   See also: http://bugzilla.gnome.org/show_bug.cgi?id=70964
  36.  *
  37.  * Changes from version 1.05 to version 1.06:
  38.  * - Fixed bug that completely white pixel (= grayscale 255) was not
  39.  *   mapped properly.  (Thanks to Dov Grobgeld)
  40.  *
  41.  * Changes from version 1.04 to version 1.05:
  42.  * - Now it uses gimp_gradients_sample_uniform () instead of blend
  43.  *   tool. Maybe right thing.
  44.  *
  45.  * Changes from revision 1.1 to version 1.04:
  46.  * - Fix bug that it didn't work with alpha channel.
  47.  * - Changed calling `gimp_blend' so that it works in v0.99.9.
  48.  * - Changed calling `gimp_blend' so that it works with Quartic's
  49.  *   asupsample patch.
  50.  * - Fix the way to calculate luminosity of RGB image.
  51.  *   (Thanks to Michael Lamertz)
  52.  */
  53.  
  54. #include "config.h"
  55.  
  56. #include <stdio.h>
  57. #include <stdlib.h>
  58.  
  59. #include <libgimp/gimp.h>
  60.  
  61. #include "libgimp/stdplugins-intl.h"
  62.  
  63.  
  64. #ifdef RCSID
  65. static char rcsid[] = "$Id: gradmap.c,v 1.11.2.1 2002/02/09 15:09:56 neo Exp $";
  66. #endif
  67.  
  68. /* Some useful macros */
  69.  
  70. #define NSAMPLES    256
  71. #define TILE_CACHE_SIZE 32
  72. #define LUMINOSITY(X)    (INTENSITY (X[0], X[1], X[2]))
  73.  
  74. /* Declare a local function.
  75.  */
  76. static void     query        (void);
  77. static void     run        (gchar     *name,
  78.                  gint     nparams,
  79.                  GimpParam     *param,
  80.                  gint     *nreturn_vals,
  81.                  GimpParam     **return_vals);
  82.  
  83. static void     gradmap    (GimpDrawable *drawable);
  84. static guchar *     get_samples    (GimpDrawable *drawable );
  85.  
  86.  
  87. GimpPlugInInfo PLUG_IN_INFO =
  88. {
  89.   NULL,     /* init_proc  */
  90.   NULL,     /* quit_proc  */
  91.   query, /* query_proc */
  92.   run,     /* run_proc   */
  93. };
  94.  
  95. MAIN ()
  96.  
  97. static void
  98. query()
  99. {
  100.   static GimpParamDef args[]=
  101.     {
  102.       { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
  103.       { GIMP_PDB_IMAGE, "image", "Input image (unused)" },
  104.       { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" }
  105.    };
  106.   static gint nargs = sizeof (args) / sizeof (args[0]);
  107.  
  108.   gimp_install_procedure ("plug_in_gradmap",
  109.                           "Map the contents of the specified drawable with "
  110.               "active gradient",
  111.                           " This plug-in maps the contents of the specified "
  112.               "drawable with active gradient. It calculates "
  113.               "luminosity of each pixel and replaces the pixel "
  114.               "by the sample of active gradient at the position "
  115.               "proportional to that luminosity. Complete black "
  116.               "pixel becomes the leftmost color of the gradient, "
  117.               "and complete white becomes the rightmost. Works on "
  118.               "both Grayscale and RGB image with/without alpha "
  119.               "channel.",
  120.               "Eiichi Takamori",
  121.               "Eiichi Takamori",
  122.               "1997",
  123.               N_("<Image>/Filters/Colors/Map/Gradient Map"),
  124.               "RGB*, GRAY*",
  125.               GIMP_PLUGIN,
  126.               nargs, 0,
  127.               args, NULL);
  128. }
  129.  
  130. static void
  131. run (gchar   *name,
  132.      gint    nparams,
  133.      GimpParam  *param,
  134.      gint    *nreturn_vals,
  135.      GimpParam  **return_vals)
  136. {
  137.   static GimpParam values[1];
  138.   GimpDrawable *drawable;
  139.   GimpRunModeType run_mode;
  140.   GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  141.  
  142.   run_mode = param[0].data.d_int32;
  143.  
  144.   INIT_I18N();
  145.  
  146.   *nreturn_vals = 1;
  147.   *return_vals = values;
  148.  
  149.   values[0].type = GIMP_PDB_STATUS;
  150.   values[0].data.d_status = status;
  151.  
  152.   /*  Get the specified drawable  */
  153.   drawable = gimp_drawable_get (param[2].data.d_drawable);
  154.  
  155.   /*  Make sure that the drawable is gray or RGB color    */
  156.   if (gimp_drawable_is_rgb (drawable->id) ||
  157.       gimp_drawable_is_gray (drawable->id))
  158.     {
  159.       gimp_progress_init ( _("Gradient Map..."));
  160.       gimp_tile_cache_ntiles (TILE_CACHE_SIZE);
  161.  
  162.       gradmap (drawable);
  163.  
  164.       if (run_mode != GIMP_RUN_NONINTERACTIVE)
  165.     gimp_displays_flush ();
  166.     }
  167.   else
  168.     {
  169.       /* g_message ("gradmap: cannot operate on indexed color images"); */
  170.       status = GIMP_PDB_EXECUTION_ERROR;
  171.     }
  172.  
  173.   values[0].data.d_status = status;
  174.  
  175.   gimp_drawable_detach (drawable);
  176. }
  177.  
  178. static void
  179. gradmap (GimpDrawable *drawable)
  180. {
  181.   GimpPixelRgn    src_rgn, dest_rgn;
  182.   gpointer    pr;
  183.   guchar    *src_row, *dest_row;
  184.   guchar    *src, *dest;
  185.   gint        progress, max_progress;
  186.   gint        x1, y1, x2, y2;
  187.   gint        row, col;
  188.   gint        bpp, color, has_alpha;
  189.   guchar    *samples, *samp;
  190.   gint        lum;                /* luminosity */
  191.   gint        b;
  192.  
  193.   gimp_drawable_mask_bounds (drawable->id, &x1, &y1, &x2, &y2);
  194.  
  195.   bpp = gimp_drawable_bpp (drawable->id);
  196.   color = gimp_drawable_is_rgb (drawable->id);
  197.   has_alpha = gimp_drawable_has_alpha (drawable->id);
  198.  
  199.   samples = get_samples (drawable);
  200.  
  201.   gimp_pixel_rgn_init (&src_rgn, drawable, x1, y1, x2-x1, y2-y1, FALSE, FALSE);
  202.   gimp_pixel_rgn_init (&dest_rgn, drawable, x1, y1, x2-x1, y2-y1, TRUE, TRUE);
  203.  
  204.   /* Initialize progress */
  205.   progress = 0;
  206.   max_progress = (x2 - x1) * (y2 - y1);
  207.  
  208.   for (pr = gimp_pixel_rgns_register (2, &src_rgn, &dest_rgn);
  209.        pr != NULL; pr = gimp_pixel_rgns_process (pr))
  210.     {
  211.       src_row = src_rgn.data;
  212.       dest_row = dest_rgn.data;
  213.  
  214.       for (row = 0; row < src_rgn.h; row++)
  215.     {
  216.       src = src_row;
  217.       dest = dest_row;
  218.  
  219.       for (col = 0; col < src_rgn.w; col++)
  220.         {
  221.           if (color)
  222.         {
  223.           lum = LUMINOSITY (src);
  224.           lum = CLAMP (lum, 0, 255);    /* to make sure */
  225.         }
  226.           else
  227.         lum = src[0];
  228.  
  229.           samp = &samples[lum * bpp];
  230.           if (has_alpha)
  231.         {
  232.           for (b = 0; b < bpp - 1; b++)
  233.             dest[b] = samp[b];
  234.           dest[b] = ((guint)(samp[b]) * (guint)(src[b])) / 255;
  235.         }
  236.           else
  237.         {
  238.           for (b = 0; b < bpp; b++)
  239.             dest[b] = samp[b];
  240.         }
  241.  
  242.           src += src_rgn.bpp;
  243.           dest += dest_rgn.bpp;
  244.         }
  245.       src_row += src_rgn.rowstride;
  246.       dest_row += dest_rgn.rowstride;
  247.     }
  248.       /* Update progress */
  249.       progress += src_rgn.w * src_rgn.h;
  250.       gimp_progress_update ((double) progress / (double) max_progress);
  251.     }
  252.  
  253.   g_free (samples);
  254.  
  255.   gimp_drawable_flush (drawable);
  256.   gimp_drawable_merge_shadow (drawable->id, TRUE);
  257.   gimp_drawable_update (drawable->id, x1, y1, (x2 - x1), (y2 - y1));
  258. }
  259.  
  260. /*
  261.   Returns 256 samples of active gradient.
  262.   Each sample has (gimp_drawable_bpp (drawable->id)) bytes.
  263.  */
  264. static guchar *
  265. get_samples (GimpDrawable *drawable)
  266. {
  267.   gdouble    *f_samples, *f_samp;    /* float samples */
  268.   guchar    *b_samples, *b_samp;    /* byte samples */
  269.   gint        bpp, color, has_alpha, alpha;
  270.   gint        i, j;
  271.  
  272.   f_samples = gimp_gradients_sample_uniform (NSAMPLES);
  273.  
  274.   bpp        = gimp_drawable_bpp (drawable->id);
  275.   color        = gimp_drawable_is_rgb (drawable->id);
  276.   has_alpha = gimp_drawable_has_alpha (drawable->id);
  277.   alpha        = (has_alpha ? bpp - 1 : bpp);
  278.  
  279.   b_samples = g_new (guchar, NSAMPLES * bpp);
  280.  
  281.   for (i = 0; i < NSAMPLES; i++)
  282.     {
  283.       b_samp = &b_samples[i * bpp];
  284.       f_samp = &f_samples[i * 4];
  285.       if (color)
  286.     for (j = 0; j < 3; j++)
  287.       b_samp[j] = f_samp[j] * 255;
  288.       else
  289.     b_samp[0] = LUMINOSITY (f_samp) * 255;
  290.  
  291.       if (has_alpha)
  292.     b_samp[alpha] = f_samp[3] * 255;
  293.     }
  294.  
  295.   g_free (f_samples);
  296.   return b_samples;
  297. }
  298.